package com.cpi.cornpp.version.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.version.exception.VersionException;
import com.cpi.cornpp.version.services.VersionService;

public class VersionDeleteAction extends Action {
	/** Identifier for version id*/
	private static final String VERSION_ID = "versionId";//$NON-NLS-1$
	/** Default forward */
	private final static String DEFAULT_FORWARD = "default";//$NON-NLS-1$
	/** Success delete key */
	private static final String SUCCESS_DELETE_LABEL = "common.delete.success";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/** Error message name*/
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	/*
	 * (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws VersionException {
		
		VersionService versionService = null;
		int versionId = 0;
		MessageResources messageResources = null;
		HttpSession httpSession = null;
		String messageError = null;
		try{
			httpSession = request.getSession(false);
			messageResources = getResources(request);
			versionId = Integer.parseInt(request.getParameter(VERSION_ID));
			versionService = new VersionService();
			versionService.delete(versionId);
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_DELETE_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);
		}catch (VersionException versionException){
			if( versionException.getErrorCode() == null ){
				httpSession.setAttribute(CUSTOM_EXCEPTION, versionException);
				throw versionException;
			}else{
				messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), versionException.getErrorCode());
				if(null == messageError){
					httpSession.setAttribute(CUSTOM_EXCEPTION, versionException);
					throw versionException;
				}else{
					httpSession.setAttribute(ERROR_MESSAGE, messageError);
				}
			}
		}catch (Exception e){
			VersionException versionException=null;
			e.printStackTrace(System.out);
			versionException= new VersionException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_DELETE);
			httpSession.setAttribute(CUSTOM_EXCEPTION, versionException);
			throw versionException;
		}			
		return mapping.findForward(DEFAULT_FORWARD);
	}
}